package com.uyayo.system.config.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.uyayo.framework.common.exception.CodeMultipleException;
import com.uyayo.framework.pagehelper.pojo.OrderBy;
import com.uyayo.framework.pagehelper.pojo.PageBean;
import com.uyayo.framework.pagehelper.util.OrderByUtil;
import com.uyayo.system.config.entity.SysParam;
import com.uyayo.system.config.mapper.SysParamMapper;
import com.uyayo.system.config.model.SysParamCondition;
import com.uyayo.system.config.service.SysParamService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;


@Service("paramsService")
public class SysParamServiceImpl implements SysParamService {

	private final SysParamMapper sysParamMapper;

	@Autowired
	public SysParamServiceImpl(SysParamMapper sysParamMapper) {
		this.sysParamMapper = sysParamMapper;
	}

	@Override
	public SysParam getSysParamsById(String id){
		return sysParamMapper.selectByPrimaryKey(id);
	}



	@Override
	public PageBean<SysParam> selectParams(Integer pageCurrent, Integer pageSize,String sort,String order,SysParamCondition sysParamQuery) {
		
		List<SysParam> params;
		//分页处理
		List<OrderBy> orderBies = new ArrayList<>();
		orderBies.add(new OrderBy("type", "type"));
		orderBies.add(new OrderBy("code", "code"));
		orderBies.add(new OrderBy("name", "name"));
		orderBies.add(new OrderBy("value", "value"));
		orderBies.add(new OrderBy("description", "description"));
		orderBies.add(new OrderBy("createdTime", "created_time"));
		orderBies.add(new OrderBy("modifiedTime", "modified_time"));
		orderBies.add(new OrderBy("remark", "remark"));
		OrderByUtil orderByUtil = new OrderByUtil();
		String orderBy = orderByUtil.getOrderBy(orderBies, sort, order);
		PageHelper.startPage(pageCurrent, pageSize);
		Example example = new Example(SysParam.class);
		if(!StringUtils.isEmpty(orderBy)){
			example.setOrderByClause(orderBy);
		}
		Criteria criteria = example.createCriteria();
		if(!StringUtils.isEmpty(sysParamQuery.getName())){
			criteria.andLike("name","%"+sysParamQuery.getName()+"%");
		}
		if(sysParamQuery.getBeginCreatedTime()!=null || sysParamQuery.getEndCreatedTime()!=null){
			criteria.andBetween("createdTime", sysParamQuery.getBeginCreatedTime(), sysParamQuery.getEndCreatedTime());
		}
		params = sysParamMapper.selectByExample(example);
		
		//取记录总条数
		PageInfo<SysParam> pageInfo = new PageInfo<>(params);
		
		return new PageBean<>(pageInfo.getTotal(), pageCurrent, pageSize, params);
	}

	
	
	
	@Override
	public List<SysParam> selectParams(SysParam params) {
		return sysParamMapper.selectParams(null, 0);
	}

	@Override
	public int saveParams(SysParam params) throws CodeMultipleException {
        SysParam sysParamTest = getParamByCode(params.getCode());
        if(sysParamTest!=null){
            throw new CodeMultipleException("Code multiple error!");
        }
		return sysParamMapper.insertSelective(params);
	}

	@Override
	public int updateParam(SysParam sysParam) throws CodeMultipleException {
		SysParam sysParamTest = getParamByCode(sysParam.getCode());
		if(sysParamTest!=null && !StringUtils.equals(sysParam.getId(), sysParamTest.getId())){
			throw new CodeMultipleException("Code multiple error!");
		}
		return sysParamMapper.updateByPrimaryKeySelective(sysParam);
	}

	@Override
	public int deleteParamsById(String id) {
		return sysParamMapper.deleteByPrimaryKey(id);
	}
	
	
	/**
	 * 根据编码获取下级字典
	 * @return String
	 */
	@Override
	public String getSysParamValueByCode(String code) {
		SysParam sysParam = new SysParam();
		sysParam.setCode(code);
		sysParam = sysParamMapper.selectOne(sysParam);
		return sysParam==null?null:sysParam.getValue();
	}



	@Override
	public SysParam getParamByCode(String code) {
		SysParam sysParam = new SysParam();
		sysParam.setCode(code);
		sysParam = sysParamMapper.selectOne(sysParam);
		return sysParam;
	}

	@Override
	public String  getUploadBaseUrl(){
		return getSysParamValueByCode("UPLOAD_BASE_URL");
	}

	@Override
	public boolean getFtpEnabled(){
		return Objects.equals("1",getSysParamValueByCode("FTP_ENABLED"));
	}

	@Override
	public String getDefaultUploadPath(){
		return getSysParamValueByCode("DEFAULT_UPLOAD_PATH");
	}

}
